package controller;

import junit.framework.TestCase;
import model.Perspective;
import model.RawImage;
import controller.Command;

public class CommandManagerTest extends TestCase {

	private String imgPath;
	private CommandManager cm;
	
	public void setUp(){
		imgPath = "resources/CIMG2530.JPG";
		cm = CommandManager.getInstance();
	}
	
	public void testGetInstance() {
		assertNotNull(cm);
	}

	public void testAddCommand() {
		Perspective p = new Perspective(new RawImage(imgPath));
		Command c1 = new TranslateCommand(p, 10, 10);
		Command c2 = new TranslateCommand(p, 10, 0);
		
		cm.executeCommand(c1);
		assertEquals(10, p.getX());
		assertEquals(10, p.getY());
		
		cm.executeCommand(c2);
		assertEquals(20, p.getX());
		assertEquals(10, p.getY());
	}

	public void testUndo() {
		Perspective p = new Perspective(new RawImage(imgPath));
		Command c1 = new TranslateCommand(p, 10, 10);
		Command c2 = new TranslateCommand(p, 10, 0);
		cm.executeCommand(c1);
		cm.executeCommand(c2);
		
		cm.undo();
		assertEquals(10, p.getX());
		assertEquals(10, p.getY());
		
		cm.undo();
		assertEquals(0, p.getX());
		assertEquals(0, p.getY());
		
		cm.undo(); //On fait un undo sur une pile vide
		assertEquals(0, p.getX());
		assertEquals(0, p.getY());
	}

	public void testRedo() {
		Perspective p = new Perspective(new RawImage(imgPath));
		Command c1 = new TranslateCommand(p, 10, 10);
		Command c2 = new TranslateCommand(p, 10, 0);
		cm.executeCommand(c1);
		cm.executeCommand(c2);
		
		cm.undo();
		cm.redo();
		assertEquals(20, p.getX());
		assertEquals(10, p.getY());
		
		cm.undo();
		cm.undo();
		cm.redo();
		assertEquals(10, p.getX());
		assertEquals(10, p.getY());
		
		cm.redo();
		assertEquals(20, p.getX());
		assertEquals(10, p.getY());
	}

}
